package test;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class JavaUrlFlash {

	    private static void log(String s)
	    {
	        System.out.println(s);
	    }

	    private static void log(Integer s)
	    {
	        System.out.println(s);
	    }

	    private static void log(boolean s)
	    {
	        System.out.println(s);
	    }

	    private static void line()
	    {
	        log("-----------------");
	    }

	    public static String getUrl(String strIP, String strPort, String urlPath)
	    {
	        int nPort = new Integer(strPort).intValue();
	        Socket s = null;
	        try
	        {
	            s = new Socket(strIP, nPort);

	            if (s.isConnected())
	            {
	                log("connected");
	            }
	            OutputStream output = s.getOutputStream();
	            StringBuilder sb = new StringBuilder();
	            sb.append("GET " + urlPath + " HTTP/1.1\r\n");
	            sb.append("Host:" + strIP + "\r\n");
	            sb.append("Connection:Close\r\n");
	            sb.append("\r\n");
	           

	            output.write(sb.toString().getBytes("utf8"));
	            log("Send:" + String.format("%n") + sb.toString());
	            output.flush();
	            
	            InputStream in = s.getInputStream();
	            BufferedInputStream bisIn = new BufferedInputStream(in);

	            log(bisIn.available());
	            BufferedReader reader = new BufferedReader(new InputStreamReader(bisIn, "gb2312"));

	            StringBuilder html = new StringBuilder();
	            if (reader.ready())
	            {
	                String line;
	                do
	                {
	                    line = reader.readLine();
	                    if (line != null)
	                    {
	                        System.out.println(line);
	                        html.append(line);
	                    }
	                } while (line != null);
	            }

	            in.close();
	            return html.toString();

	        } catch (UnknownHostException e)
	        {
	            e.printStackTrace();
	            return null;
	        } catch (IOException ex)
	        {
	            ex.printStackTrace();
	            return null;
	        } finally
	        {
	            try
	            {
	                s.close();
	            } catch (IOException ex2)
	            {
	                ex2.printStackTrace();
	            }
	        }
	    }

	    public static void main(String[] args)
	    {
	    	JavaUrlFlash.getUrl("finance.sina.com.cn", "80", "/realstock/company/sh600316/nc.shtml");    }
}
